<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划编号" prop="planCode">
        <el-input v-model="queryParams.planCode" placeholder="请输入计划编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="queryParams.planName" placeholder="请输入计划名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <treeselect v-model="queryParams.deptId" style="width: 150px;" :options="deptOptions" :show-count="true" :clearable="false"
          placeholder="请选择归属部门" :normalizer="normalizer" />

      </el-form-item>
      <el-form-item label="负责人" prop="userId">

        <el-select v-model="queryParams.userId" filterable remote reserve-keyword placeholder="请输入负责人"
          style="width: 150px;" :remote-method="remoteMethod" :loading="userLoading">
          <el-option v-for="item in userList" :key="item.id" :label="item.emqName" :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>

      <el-form-item label="关联设备" prop="imei">
        <el-input v-model="queryParams.imei" placeholder="请输入关联设备" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="是否启用" prop="planEnable">
        <el-select v-model="queryParams.planEnable" placeholder="请选择是否启用">
          <el-option v-for="item in planEnableList" :key="item.value" :label="item.lable" :value="item.value">
          </el-option>
          </el-select>
      </el-form-item>

      <el-form-item label="审核状态" prop="userId">
        <el-select v-model="queryParams.planState" placeholder="请选择审核状态">
            <el-option v-for="item in stateList" :key="item.value" :label="item.lable" :value="item.value">
            </el-option>
          </el-select>

      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">

      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="计划编号" align="center" width="180" prop="planCode" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="负责人" align="center" prop="nickname" />
      <el-table-column label="巡检周期" align="center" prop="planPeriodVal" >
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planPeriodVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务有效时间" align="center" prop="planValidTimeVal" width="180" >
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planValidTimeVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提前生成时间" align="center" prop="planAdvanceTimeVal" width="180">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planAdvanceTimeVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提前提醒时间" align="center" prop="planRemindTimeVal" width="180">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planRemindTimeVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划开始时间" align="center" prop="planBeginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planBeginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="planEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次生成时间" align="center" prop="planLastTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planLastTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下次生成时间" align="center" prop="planNextTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planNextTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="planEnable" >
        <template slot-scope="scope">
          <span>{{ scope.row.planEnable==1?"是":"否" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="createTime"  width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前节点" align="center" prop="planNode" />
      <el-table-column label="审批状态" align="center" prop="planState" >
        <template slot-scope="scope">
          <span>{{selectStateLabel(scope.row.planState) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批结束时间" align="center" prop="planApproveEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planApproveEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['abu:polling-plan:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['abu:polling-plan:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
  import {
    createPollingPlan,
    updatePollingPlan,
    deletePollingPlan,
    getPollingPlan,
    getPollingPlanPage,
    exportPollingPlanExcel,getPollingPlanMyList 
  } from "@/api/polling/pollingPlan";
  import {
    getUserList,
    getStandardPage
  } from "@/api/polling/standard"
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import Treeselect from "@riophae/vue-treeselect";
  import {
    listSimpleDepts
  } from "@/api/system/dept";
  export default {
    name: "PollingPlan",
    components: {
      Treeselect
    },
    data() {
      return {
        userLoading: false,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 巡检计划列表
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          creator:"my",
          planCode: null,
          planName: null,
          deptId: null,
          userId: null,
          planEnable: null,
          planState: null,
        },
        // 表单参数
        form: {
          planEnable: 0
        },
        // 表单校验
        rules: {},
        userName: "",
        deptOptions: [],
        userList: [],
        standardList: [],
        stateList:[
         {
           id:1,
           value:0,
           lable:"审核中"
         },
         {
           id:2,
           value:1,
           lable:"通过"
         }
        ],
        planEnableList:[
          {
            id:1,
            value:0,
            lable:"否"
          },
          {
            id:2,
            value:1,
            lable:"是"
          }
        ],
        dataSectionList: [ //放数据字典
          {
            id: 0,
            lable: "1时"
          },
          {
            id: 1,
            lable: "3时"
          },
          {
            id: 2,
            lable: "1日"
          },
          {
            id: 3,
            lable: "3日"
          },
          {
            id: 4,
            lable: "1周"
          },
          {
            id: 5,
            lable: "1月"
          },
          {
            id: 6,
            lable: "3月"
          },
          {
            id: 7,
            lable: "6月"
          },
          {
            id: 8,
            lable: "12月"
          }
        ]

      };
    },
    created() {
      this.initData();
      this.getList();
    },
    methods: {
      selectStateLabel(value) {
        var datas=this.stateList;
       let index = datas.findIndex(item=> item.value == value)
       	if(index != -1){
       		return datas[index].lable;
       	}
       	return value;
       },
     selectDictLabel(value) {
       var datas=this.dataSectionList;
      let index = datas.findIndex(item=> item.id == value)
      	if(index != -1){
      		return datas[index].lable;
      	}
      	return value;
      },

      getLableType(item) {
        return item.pollingName + "[" + item.pollingCode + "]";
      },
      remoteMethod(query) {
        if (query !== '') {
          this.userName = query
          this.getUserListQuery();
        } else {
          this.userList = [];
        }
      },
      getUserListQuery() {
        this.userLoading = true;
        getUserList({
          equName: this.userName
        }).then(res => {
          this.userLoading = false;
          this.userList = res.data;
          console.log("getUserList", res)
        });
      },

      initData() {
        this.getUserListQuery();
        getStandardPage({
          pageNo: 1,
          pageSize: 50
        }).then(res => {
          this.standardList = res.data.list;
          console.log("getStandardPage", res)
        });
        this.getTreeselect();
      },
      getTreeselect() {
        listSimpleDepts().then(response => {
          // 处理 deptOptions 参数
          this.deptOptions = [];
          this.deptOptions.push(...this.handleTree(response.data, "id"));
        });
      },
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        // 执行查询
        //getPollingPlanPage(this.queryParams).then(response => {
          getPollingPlanMyList(this.queryParams).then(response => {
          this.list = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNo = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      }

    }
  };
</script>
